﻿using Inovout.Services;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Inovout.NameCodeDictionary
{
    public static class NameCodeDictionaryManager 
    {
        public static IEnumerable<NameCode> FindAll(string scope, string key)
        {
            IRepositoryService<NameCode> namecodeService = ComponentRegistry.Resolve<IRepositoryService<NameCode>>(); 
            return namecodeService.Query().Where(nc => nc.Name.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0 && nc.QuanPinName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0 && nc.JianPinName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0
                                                    && nc.ShortName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0 && nc.JianPinName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0 && nc.JianPinShortName.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0
                                                    && nc.Collection.Scope.Equals(scope, StringComparison.OrdinalIgnoreCase) && nc.Collection.IsValid);  
        }

        public static IEnumerable<NameCode> FindByCollectionName(string name)
        {
            IRepositoryService<NameCode> namecodeService = ComponentRegistry.Resolve<IRepositoryService<NameCode>>();

            return namecodeService.Query().Where(nc => nc.Collection.Name == name).ToList<NameCode>();
        }
    }
}
